package architecture.diagram.navigator;

import org.eclipse.gmf.runtime.common.ui.services.parser.IParser;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions;
import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITreePathLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.ViewerLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.navigator.ICommonContentExtensionSite;
import org.eclipse.ui.navigator.ICommonLabelProvider;

import architecture.Architecture;
import architecture.diagram.edit.parts.ArchitectureEditPart;
import architecture.diagram.edit.parts.FeatureDescriptionEditPart;
import architecture.diagram.edit.parts.FeatureEditPart;
import architecture.diagram.edit.parts.FeatureIsRealicedByEditPart;
import architecture.diagram.edit.parts.ModuleComplexityEditPart;
import architecture.diagram.edit.parts.ModuleDependEditPart;
import architecture.diagram.edit.parts.ModuleEditPart;
import architecture.diagram.edit.parts.ModuleImplementEditPart;
import architecture.diagram.edit.parts.ResponsabilityEditPart;
import architecture.diagram.edit.parts.ResponsabilityNameEditPart;
import architecture.diagram.part.ArchitectureDiagramEditorPlugin;
import architecture.diagram.part.ArchitectureVisualIDRegistry;
import architecture.diagram.providers.ArchitectureElementTypes;
import architecture.diagram.providers.ArchitectureParserProvider;

/**
 * @generated
 */
public class ArchitectureNavigatorLabelProvider extends LabelProvider implements
		ICommonLabelProvider, ITreePathLabelProvider {

	/**
	 * @generated
	 */
	static {
		ArchitectureDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?UnknownElement", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
		ArchitectureDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?ImageNotFound", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	public void updateLabel(ViewerLabel label, TreePath elementPath) {
		Object element = elementPath.getLastSegment();
		if (element instanceof ArchitectureNavigatorItem
				&& !isOwnView(((ArchitectureNavigatorItem) element).getView())) {
			return;
		}
		label.setText(getText(element));
		label.setImage(getImage(element));
	}

	/**
	 * @generated
	 */
	public Image getImage(Object element) {
		if (element instanceof ArchitectureNavigatorGroup) {
			ArchitectureNavigatorGroup group = (ArchitectureNavigatorGroup) element;
			return ArchitectureDiagramEditorPlugin.getInstance()
					.getBundledImage(group.getIcon());
		}

		if (element instanceof ArchitectureNavigatorItem) {
			ArchitectureNavigatorItem navigatorItem = (ArchitectureNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return super.getImage(element);
			}
			return getImage(navigatorItem.getView());
		}

		return super.getImage(element);
	}

	/**
	 * @generated
	 */
	public Image getImage(View view) {
		switch (ArchitectureVisualIDRegistry.getVisualID(view)) {
		case FeatureIsRealicedByEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://architecture/1.0?Feature?isRealicedBy", ArchitectureElementTypes.FeatureIsRealicedBy_4001); //$NON-NLS-1$
		case ModuleDependEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://architecture/1.0?Module?Depend", ArchitectureElementTypes.ModuleDepend_4002); //$NON-NLS-1$
		case FeatureEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://architecture/1.0?Feature", ArchitectureElementTypes.Feature_2003); //$NON-NLS-1$
		case ArchitectureEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Diagram?http://architecture/1.0?Architecture", ArchitectureElementTypes.Architecture_1000); //$NON-NLS-1$
		case ResponsabilityEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://architecture/1.0?Responsability", ArchitectureElementTypes.Responsability_2002); //$NON-NLS-1$
		case ModuleEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://architecture/1.0?Module", ArchitectureElementTypes.Module_2001); //$NON-NLS-1$
		case ModuleImplementEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://architecture/1.0?Module?Implement", ArchitectureElementTypes.ModuleImplement_4003); //$NON-NLS-1$
		}
		return getImage("Navigator?UnknownElement", null); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private Image getImage(String key, IElementType elementType) {
		ImageRegistry imageRegistry = ArchitectureDiagramEditorPlugin
				.getInstance().getImageRegistry();
		Image image = imageRegistry.get(key);
		if (image == null && elementType != null
				&& ArchitectureElementTypes.isKnownElementType(elementType)) {
			image = ArchitectureElementTypes.getImage(elementType);
			imageRegistry.put(key, image);
		}

		if (image == null) {
			image = imageRegistry.get("Navigator?ImageNotFound"); //$NON-NLS-1$
			imageRegistry.put(key, image);
		}
		return image;
	}

	/**
	 * @generated
	 */
	public String getText(Object element) {
		if (element instanceof ArchitectureNavigatorGroup) {
			ArchitectureNavigatorGroup group = (ArchitectureNavigatorGroup) element;
			return group.getGroupName();
		}

		if (element instanceof ArchitectureNavigatorItem) {
			ArchitectureNavigatorItem navigatorItem = (ArchitectureNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return null;
			}
			return getText(navigatorItem.getView());
		}

		return super.getText(element);
	}

	/**
	 * @generated
	 */
	public String getText(View view) {
		if (view.getElement() != null && view.getElement().eIsProxy()) {
			return getUnresolvedDomainElementProxyText(view);
		}
		switch (ArchitectureVisualIDRegistry.getVisualID(view)) {
		case FeatureIsRealicedByEditPart.VISUAL_ID:
			return getFeatureIsRealicedBy_4001Text(view);
		case ModuleDependEditPart.VISUAL_ID:
			return getModuleDepend_4002Text(view);
		case FeatureEditPart.VISUAL_ID:
			return getFeature_2003Text(view);
		case ArchitectureEditPart.VISUAL_ID:
			return getArchitecture_1000Text(view);
		case ResponsabilityEditPart.VISUAL_ID:
			return getResponsability_2002Text(view);
		case ModuleEditPart.VISUAL_ID:
			return getModule_2001Text(view);
		case ModuleImplementEditPart.VISUAL_ID:
			return getModuleImplement_4003Text(view);
		}
		return getUnknownElementText(view);
	}

	/**
	 * @generated
	 */
	private String getModule_2001Text(View view) {
		IParser parser = ArchitectureParserProvider.getParser(
				ArchitectureElementTypes.Module_2001,
				view.getElement() != null ? view.getElement() : view,
				ArchitectureVisualIDRegistry
						.getType(ModuleComplexityEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			ArchitectureDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5001); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getArchitecture_1000Text(View view) {
		Architecture domainModelElement = (Architecture) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			ArchitectureDiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 1000); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getModuleImplement_4003Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getResponsability_2002Text(View view) {
		IParser parser = ArchitectureParserProvider.getParser(
				ArchitectureElementTypes.Responsability_2002,
				view.getElement() != null ? view.getElement() : view,
				ArchitectureVisualIDRegistry
						.getType(ResponsabilityNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			ArchitectureDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5004); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getFeature_2003Text(View view) {
		IParser parser = ArchitectureParserProvider.getParser(
				ArchitectureElementTypes.Feature_2003,
				view.getElement() != null ? view.getElement() : view,
				ArchitectureVisualIDRegistry
						.getType(FeatureDescriptionEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			ArchitectureDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5006); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getFeatureIsRealicedBy_4001Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getModuleDepend_4002Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getUnknownElementText(View view) {
		return "<UnknownElement Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	private String getUnresolvedDomainElementProxyText(View view) {
		return "<Unresolved domain element Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	public void init(ICommonContentExtensionSite aConfig) {
	}

	/**
	 * @generated
	 */
	public void restoreState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public void saveState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public String getDescription(Object anElement) {
		return null;
	}

	/**
	 * @generated
	 */
	private boolean isOwnView(View view) {
		return ArchitectureEditPart.MODEL_ID
				.equals(ArchitectureVisualIDRegistry.getModelID(view));
	}

}
